package com.egdd.video.controller;

import com.egdd.video.common.api.ApiResult;
import com.egdd.video.config.ApiConfig;
import com.egdd.video.config.ServerConfig;
import com.egdd.video.controller.common.BaseController;
import com.egdd.video.pojo.Song;
import com.egdd.video.service.SongService;
import com.egdd.video.utils.FileUploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;

/**
 * @Author: wh
 * @Date: 2021/2/23 14:32
 */

@RestController
@RequestMapping("/song")
@Api(tags = {"音频管理"})
public class SongController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(Song.class);

    @Autowired
    private SongService songService;

    @Autowired
    private ServerConfig serverConfig;


    @ApiOperation("根据分类id获取音频合集列表信息")
    @GetMapping(value = "/getSongByCategoryId")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "音频分类id", defaultValue = "258"),
                    @ApiImplicitParam(name = "page", value = "当前页码", defaultValue = "1"),
                    @ApiImplicitParam(name = "pageSize", value = "页码大小", defaultValue = "10")
            }
    )
    public ApiResult getAlbumsByCategoryId(Integer id, int page, int pageSize) {
        return ApiResult.success(songService.getSongByCategoryId(id, page, pageSize));
    }


    @ApiOperation("上传儿歌")
    @PostMapping("/uploadSong")
    public ApiResult uploadSong(@RequestParam("file") MultipartFile file) throws IOException {

        // 上传文件路径
        String filePath = ApiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        String url = serverConfig.getUrl() + fileName;
        ApiResult data = ApiResult.success();
        Song song = new Song();
        song.setAudiourl(url);
        song.setName(fileName);
        //保存数据库
        songService.save(song);
        //返回前端
        data.put("fileName", fileName);
        data.put("url", url);
        return data;
    }

}
